package com.chenyongjing.design.pattern.strategy.pay;

import com.chenyongjing.design.pattern.strategy.pay.payport.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 策略模式，处理if...else...
 * @Author chen
 * @Date 2022/11/13 22:05
 */
public class PayStrategy {
    public static final String ALI_PAY = "AliPay";
    public static final String JD_PAY = "JDPay";
    public static final String WECHAT_PAY = "WechatPay";
    public static final String UNION_PAY = "UnionPay";
    public static final String DEFAULT_PAY = ALI_PAY;

    private static Map<String,Payment> payStrategy = new HashMap<String, Payment>();

    // 初始化Map
    static {
        payStrategy.put(ALI_PAY, new AliPay());
        payStrategy.put(JD_PAY,new JDPay());
        payStrategy.put(WECHAT_PAY,new WechatPay());
        payStrategy.put(UNION_PAY,new UnionPay());
    }

    // 从这里处理多个if...else和switch的处理
    public static Payment get(String payKey){
        if(payStrategy.containsKey(payKey)){
            return payStrategy.get(payKey);
        }
        return payStrategy.get(DEFAULT_PAY);
    }
}
